package com.example.tests;

import static com.example.tests.ContactDataGenerator.loadContactsFromCsvFile;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.example.fw.ContactHelper;
import com.example.utils.SortedListOf;

public class ContactCreationTests extends TestBase {
	
	@DataProvider
	public Iterator<Object[]> contactsFromFile() throws IOException{
			return wrapContactDataForProvider(loadContactsFromCsvFile(new File("contacts.txt"))).iterator();
			}
	
  @Test(dataProvider = "contactsFromFile")
  public void testValidContactCreation(ContactData contact) throws Exception {
	
	// save old state
	SortedListOf<ContactData> oldList = app.getModel().getContacts();
	
	// actions
	app.getContactHelper().createContact(contact, ContactHelper.CREATION);
    
    // save new state
	SortedListOf<ContactData> newList = app.getModel().getContacts();
    
    //compare state
    assertThat(newList, equalTo(oldList.withAdded(contact)));
    if (wantToCheck()){
        if ("yes".equals(app.getProperty("check.db"))){
                assertThat(newList, equalTo(app.getHibernateHelper().listContacts().withAdded(contact)));
        }
        if ("yes".equals(app.getProperty("check.ui"))){
                assertThat(newList, equalTo(app.getContactHelper().getUiContact().withAdded(contact)));
        }
}
  }
  

}
